﻿using InfonavaEye.util;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Net;
using System.Net.Sockets;
using System.Runtime.InteropServices;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace InfonavaEye
{
    public partial class ClientForm : Form
    {
        public ClientForm()
        {
            InitializeComponent();
        }
        private void connect(string fileName)
        {
            try
            {
                //1. 初始化套接字
                Socket ClientSocket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
                //2. 连接服务器
                IPAddress ip = IPAddress.Parse("127.0.0.1");
                int port = 8869;
                ClientSocket.Connect(new IPEndPoint(ip, port));
                //MessageBox.Show("成功与8869建立连接");


                var basePath = "D:\\project\\code\\infynova-eye-desk\\InfonavaEye\\bin\\x64\\Debug\\Image\\2022-09-09\\1";
                byte[] buffer = Encoding.UTF8.GetBytes(basePath + "\\" + fileName);
                ClientSocket.Send(buffer);

                byte[] receiveBuffer = new byte[1024];
                int counts = ClientSocket.Receive(receiveBuffer);
                if (counts > 0)
                {
                    Invoke(new MethodInvoker(delegate ()
                    {
                        Console.WriteLine("收到数据 {0}", DateTime.Now.ToString("mm:ss:fff"));
                        textBox1.Text += System.Text.Encoding.GetEncoding("GB2312").GetString(receiveBuffer, 0, counts)+"\\r\\n";
                        ClientSocket.Close();
                    }));
                }
            }
            catch (Exception)
            {
                MessageBox.Show("连接失败，请检查是否启动了服务器", "提示", MessageBoxButtons.OK, MessageBoxIcon.Error);
                //throw;
            }
        }

        private void button2_Click(object sender, EventArgs e)
        {

        }
        private void button3_Click(object sender, EventArgs e)
        {
            Console.WriteLine("开始1 {0}",DateTime.Now.ToString("mm:ss:fff"));
            new Thread(p => {
                connect("160639_1.jpg");
            }).Start();

            new Thread(p => {
                connect("160658_2.jpg");
            }).Start();

            new Thread(p => {
                connect("160748_3.jpg");
            }).Start();

            new Thread(p => {
                connect("160816_4.jpg");
            }).Start();

            new Thread(p =>
            {
                connect("160832_5.jpg");
            }).Start();

            new Thread(p =>
            {
                connect("160848_6.jpg");
            }).Start();

            new Thread(p =>
            {
                connect("160921_7.jpg");
            }).Start();

            Console.WriteLine("开始2 {0}", DateTime.Now.ToString("mm:ss:fff"));
            return;

            /*try
            {
                //定义一个文件打开控件
                OpenFileDialog ofd = new OpenFileDialog();
                //设置打开对话框的初始目录，默认目录为exe运行文件所在的路径
                ofd.InitialDirectory = Application.StartupPath;
                //设置打开对话框的标题
                ofd.Title = "请选择要打开的文件";
                //设置打开对话框可以多选
                //ofd.Multiselect = true;
                //设置对话框打开的文件类型
                //ofd.Filter = "文本文件|*.txt|音频文件|*.wav|图片文件|*.jpg|所有文件|*.*";
                //设置文件对话框当前选定的筛选器的索引
                //ofd.FilterIndex = 2;
                //设置对话框是否记忆之前打开的目录
                //ofd.RestoreDirectory = true;
                if (ofd.ShowDialog() == DialogResult.OK)
                {
                    //获取用户选择的文件完整路径
                    string filePath = ofd.FileName;
                    //获取对话框中所选文件的文件名和扩展名，文件名不包括路径
                    //string fileName = ofd.SafeFileName;
                    //MessageBox.Show(filePath);
                    if (isConnect)
                    {
                        byte[] buffer = Encoding.UTF8.GetBytes(filePath);
                        ClientSocket.Send(buffer);
                        MessageBox.Show("已成功发送");
                    }
                    else
                    {
                        MessageBox.Show("客户端未连接到服务器", "提示", MessageBoxButtons.OK, MessageBoxIcon.Error);
                    }
                }
            }
            catch (Exception ex)
            {
                Console.WriteLine("发生异常{0}{1}", ex.Message, ex.StackTrace);
                MessageBox.Show("发生异常，" + ex.Message);
            }*/
        }
    }
}
